package com.crecheng.shop.controller;

import com.alibaba.fastjson.JSON;
import com.crecheng.shop.DAO.ItemSpecificData;
import com.crecheng.shop.DAO.UserOderData;
import com.crecheng.shop.Manage.ItemSpecificManage;
import com.crecheng.shop.Manage.UserOderManage;
import com.crecheng.shop.util.MyMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/itemSp")
public class ItemSpecificController {

    @RequestMapping("/get")
    public @ResponseBody
    String get(@RequestParam(value = "id", defaultValue = "-1") int id,
               @RequestParam(value = "pid", defaultValue = "-1") int pid) {
        if (pid < 1) {
            if (id < 1)
                return MyMessage.defaultError;
            else
                return JSON.toJSONString(ItemSpecificManage.get(id));
        } else
            return JSON.toJSONString(ItemSpecificManage.getByParent(pid));
    }

    @RequestMapping("/getByName")
    public @ResponseBody
    String getByName(@RequestParam(value = "name", defaultValue = "") String name,
                     @RequestParam(value = "offset", defaultValue = "-1") int offset,
                     @RequestParam(value = "count", defaultValue = "-1") int count) {

        if(name.trim().length()<1||offset<0||count<1)
            return MyMessage.defaultError;
        return JSON.toJSONString(ItemSpecificManage.getLikeName(name,offset,count));
    }



    @RequestMapping("/add")
    public @ResponseBody
    String add(@RequestParam(value = "pid", defaultValue = "-1") int pid,
               @RequestParam(value = "price", defaultValue = "-1") float price,
               @RequestParam(value = "sale", defaultValue = "-1") int sale,
               @RequestParam(value = "have", defaultValue = "-1") int have,
               @RequestParam(value = "other", defaultValue = "") String other) {
        if (pid < 1 || price <= 0 || sale < 0 || have < 0 || other.trim().length() < 1)
            return MyMessage.defaultError;
        ItemSpecificData data = new ItemSpecificData();
        data.set(pid, other, price, sale, have);

        if (ItemSpecificManage.add(data))
            return MyMessage.defaultSuccess;
        else
            return MyMessage.defaultSqlError;
    }

    @RequestMapping("/buy")
    public @ResponseBody
    String buy(@RequestParam(value = "id", defaultValue = "-1") int id,
               @RequestParam(value = "uid", defaultValue = "-1") int uid,
               @RequestParam(value = "num", defaultValue = "-1") int num,
               @RequestParam(value = "aid", defaultValue = "-1") int aid,
               @RequestParam(value = "psMethod", defaultValue = "") String psMethod,
               @RequestParam(value = "pay", defaultValue = "") String pay) {
        if (uid < 1 || id < 1 || num < 1 || aid < 1 ||
                psMethod.trim().length() < 1 || pay.trim().length() < 1)
            return MyMessage.defaultError;

        var d = ItemSpecificManage.get(id);
        if (d != null) {
            if (ItemSpecificManage.buy(id, num)) {
                UserOderData data = new UserOderData();
                data.set(uid, id, num, aid, psMethod, pay);
                if (UserOderManage.add(data)) {

                    return MyMessage.defaultSuccess;
                } else
                    return MyMessage.defaultSqlError;
            }
        }
            return MyMessage.defaultSqlNotFoundError;
    }
}
